import request from '@/utils/request'

const userApi = process.env.VUE_APP_SYS_API + '/user'

/**
 * 查询用户
 * @param  分页参数
 * @returns {AxiosPromise}
 */
export const getUser = (params) => {
  return request({
    url: `${userApi}`,
    method: 'GET',
    params
  })
}

/**
 * 根据ID获取用户信息
 * @param id 用户id
 * @returns {AxiosPromise}
 */
export const getUserById = (id) => {
  return request({
    url: `${userApi}/${id}`,
    method: 'GET'
  })
}

/**
 * 修改用户基础信息
 * @param data 用户信息
 * @returns {AxiosPromise}
 */
export const updateUser = (data) => {
  return request({
    url: `${userApi}/${data.id}`,
    method: 'PUT',
    data
  })
}

/**
 * 更改用户状态
 * @param id
 * @param status
 * @returns {AxiosPromise}
 */
export const changeUserStatus = (id, status) => {
  let data = {
    id,
    status
  }
  return request({
    url: `${userApi}/changeStatus`,
    method: 'PUT',
    data
  })
}

/**
 * 添加新用户
 * @param data 用户信息
 * @returns {AxiosPromise}
 */
export const addUser = (data) => {
  return request({
    url: `${userApi}`,
    method: 'POST',
    data
  })
}

/**
 * 批量删除用户
 * @param ids 删除用户的id数组
 * @returns {AxiosPromise}
 */
export const delUser = (ids) => {
  return request({
    url: `${userApi}/batch/${ids}`,
    method: 'DELETE',
  })
}

/**
 * 修改密码
 * @param password 旧密码
 * @param newPassword 新密码
 * @returns {AxiosPromise}
 */
export const updateUserPwd = (password, newPassword) => {
  const data = {
    password,
    newPassword
  }
  return request({
    url: `${userApi}/updatePwd`,
    method: 'POST',
    data
  })
}

/**
 * 重置密码
 * @param id 用户id
 * @param password 密码
 * @returns {AxiosPromise}
 */
export const resetUserPwd = (id, password) => {
  const data = {
    id,
    password
  }
  return request({
    url: `${userApi}/resetPwd`,
    method: 'POST',
    data
  })
}
